package com.wonder.taxi.api;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONObject;
import org.geojson.Feature;
import org.geojson.FeatureCollection;
import org.geojson.Point;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wonder.gis.model.GeometryType;

@Path("")
public class GISService extends GISBaseService {

	@GET
	@Path("report/GetSegmentLineString")
	public Response getSegmentLineString(
			@QueryParam("segment_id") String segment_id) throws Exception
	{
		segment_id = segment_id.replaceAll(",", "");
		JSONObject res = new JSONObject();
		StringBuilder sb = new StringBuilder();
		ResultSet rs = executeSql("call nj_bus.get_segment_lng_lat(?)",
				segment_id);
		sb.append("LINESTRING (");
		while (rs.next()) {
			sb.append(rs.getDouble("from_lng"));
			sb.append(' ');
			sb.append(rs.getDouble("from_lat"));
			sb.append(',');
			sb.append(rs.getDouble("to_lng"));
			sb.append(' ');
			sb.append(rs.getDouble("to_lat"));
			sb.append(')');
		}
		res.put("result", sb.toString());
		closeResultSetResource(rs);
		Calendar expires = Calendar.getInstance();
		expires.add(Calendar.YEAR, 1);
		return buildResponse(res, "application/json", expires.getTime());
	}

	@GET
	@Path("gis/GetNode")
	public Response getNode(@QueryParam("node_id") String id) throws Exception
	{
		id = id.replaceAll(",", "");
		JSONObject res = new JSONObject();
		CallableStatement stmt = createCallStatement("call taxi.get_node(?)",
				id);
		stmt.execute();
		ResultSet rs = stmt.getResultSet();
		FeatureCollection featureCollection = new FeatureCollection();
		Feature feature = new Feature();
		featureCollection.add(feature);
		Point point = (Point) getGeometry(GeometryType.POINT, rs);
		feature.setGeometry(point);
		feature.setProperty("lng", point.getCoordinates().getLongitude());
		feature.setProperty("lat", point.getCoordinates().getLatitude());
		stmt.getMoreResults();
		rs = stmt.getResultSet();
		while (rs.next())
			feature.setProperty(rs.getString("k"), rs.getString("v"));
		closeResultSetResource(rs);
		ObjectMapper mapper = new ObjectMapper();
		res.put("result", mapper.writeValueAsString(featureCollection));
		Calendar expires = Calendar.getInstance();
		expires.add(Calendar.YEAR, 1);
		return buildResponse(res, "application/json", expires.getTime());
	}

	@GET
	@Path("gis/GetWay")
	public Response getWay(@QueryParam("way_id") String id) throws Exception
	{
		id = id.replaceAll(",", "");
		JSONObject res = new JSONObject();
		CallableStatement stmt = createCallStatement("call taxi.get_way(?)", id);
		stmt.execute();
		ResultSet rs = stmt.getResultSet();
		FeatureCollection featureCollection = new FeatureCollection();
		Feature feature = new Feature();
		featureCollection.add(feature);
		feature.setGeometry(getGeometry(GeometryType.LINE_STRING, rs));
		stmt.getMoreResults();
		rs = stmt.getResultSet();
		while (rs.next())
			feature.setProperty(rs.getString("k"), rs.getString("v"));
		closeResultSetResource(rs);
		ObjectMapper mapper = new ObjectMapper();
		res.put("result", mapper.writeValueAsString(featureCollection));
		Calendar expires = Calendar.getInstance();
		expires.add(Calendar.YEAR, 1);
		return buildResponse(res, "application/json", expires.getTime());
	}

	@GET
	@Path("gis/GetWayByName")
	public Response getWayByName(@QueryParam("way_name") String way_name)
			throws Exception
	{
		JSONObject res = new JSONObject();
		CallableStatement stmt = createCallStatement(
				"call taxi.get_way_by_name(?)", way_name);
		stmt.execute();
		ResultSet rs = stmt.getResultSet();
		FeatureCollection featureCollection = new FeatureCollection();
		Feature feature = new Feature();
		featureCollection.add(feature);
		feature.setGeometry(getGeometry(GeometryType.LINE_STRING, rs));
		stmt.getMoreResults();
		rs = stmt.getResultSet();
		while (rs.next())
			feature.setProperty(rs.getString("k"), rs.getString("v"));
		closeResultSetResource(rs);
		ObjectMapper mapper = new ObjectMapper();
		res.put("result", mapper.writeValueAsString(featureCollection));
		Calendar expires = Calendar.getInstance();
		expires.add(Calendar.YEAR, 1);
		return buildResponse(res, "application/json", expires.getTime());
	}

	@GET
	@Path("report/GetSegmentsLngLat")
	public Response getSegmentLngLat(@QueryParam("segment_id") String segment_id)
			throws Exception
	{
		JSONObject res = new JSONObject();
		segment_id = segment_id.replaceAll("\\[", "");
		segment_id = segment_id.replaceAll("\\]", "");
		String segments[] = segment_id.split(",");
		PreparedStatement segment_info_stmt = createSqlStatement("call nj_bus.get_segment_lng_lat(?)");
		StringBuilder sb = new StringBuilder(1 << 9);
		sb.append("LineString(");
		for (int i = 0; i < 1; ++i) {
			segment_info_stmt.setLong(1, Long.parseLong(segments[i]));
			segment_info_stmt.execute();
			ResultSet rs = segment_info_stmt.getResultSet();
			if (rs.next()) {
				sb.append(rs.getString("from_lng"));
				sb.append(' ');
				sb.append(rs.getString("from_lat"));
				sb.append(",");
				sb.append(rs.getString("to_lng"));
				sb.append(' ');
				sb.append(rs.getString("to_lat"));
			}
		}
		for (int i = 1; i < segments.length; ++i) {
			segment_info_stmt.setLong(1, Long.parseLong(segments[i]));
			segment_info_stmt.execute();
			ResultSet rs = segment_info_stmt.getResultSet();
			if (rs.next()) {
				sb.append(",");
				sb.append(rs.getString("from_lng"));
				sb.append(' ');
				sb.append(rs.getString("from_lat"));
				sb.append(",");
				sb.append(rs.getString("to_lng"));
				sb.append(' ');
				sb.append(rs.getString("to_lat"));
			}
		}
		sb.append(")");
		res.put("result", sb.toString());
		Calendar expires = Calendar.getInstance();
		expires.add(Calendar.YEAR, 1);
		return buildResponse(res, "application/json", expires.getTime());
	}
}